package cn.sc.pai.test;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

/**
 * description：
 * author：pany
 * on 2018/9/19 11:05
 */
public class SwipebackActivity extends AppCompatActivity {

    private final ViewGroup.LayoutParams swipeBackParams =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    protected SwipeBackLayout swipeBackLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().getDecorView().setBackgroundDrawable(null);
        swipeBackLayout = new SwipeBackLayout(this);
        swipeBackLayout.setLayoutParams(swipeBackParams);
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = LayoutInflater.from(this).inflate(layoutResID, null);
        TypedArray a = getTheme().obtainStyledAttributes(new int[]{
                android.R.attr.windowBackground
        });
        int background = a.getResourceId(0, 0);
        a.recycle();
        view.setBackgroundResource(background);
        swipeBackLayout.addView(view);
        super.setContentView(swipeBackLayout);
    }

    @Override
    public void setContentView(View view) {
        TypedArray a = getTheme().obtainStyledAttributes(new int[]{
                android.R.attr.windowBackground
        });
        int background = a.getResourceId(0, 0);
        a.recycle();
        view.setBackgroundResource(background);
        swipeBackLayout.addView(view);
        super.setContentView(swipeBackLayout);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        TypedArray a = getTheme().obtainStyledAttributes(new int[]{
                android.R.attr.windowBackground
        });
        int background = a.getResourceId(0, 0);
        a.recycle();
        view.setBackgroundResource(background);
        swipeBackLayout.addView(view, params);
        super.setContentView(swipeBackLayout);
    }

    private float x = 0;//上一次

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        float distance = 0;
        if (MotionEvent.ACTION_DOWN == action) {
            x = event.getX();
        } else if (MotionEvent.ACTION_MOVE == action) {
            distance = event.getX() - x;
            x = event.getX();
            swipeBackLayout.scrollBy((int) -distance, 0);
        } else if (MotionEvent.ACTION_UP == action) {
            if (Math.abs(swipeBackLayout.getScrollX()) < swipeBackLayout.getWidth() * 0.3f) {
                startBeginAnimate();
            } else {
                startEndAnimate();
            }
        }
        return super.onTouchEvent(event);
    }

    private void startBeginAnimate() {
        ValueAnimator animator = ValueAnimator.ofInt(swipeBackLayout.getScrollX(), 0);
        animator.setDuration(400);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                swipeBackLayout.scrollTo(value, 0);
            }
        });
        animator.start();
    }

    private void startEndAnimate() {
        ValueAnimator animator = ValueAnimator.ofInt(swipeBackLayout.getScrollX(), -swipeBackLayout.getWidth());
        animator.setDuration(400);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                swipeBackLayout.scrollTo(value, 0);
            }
        });
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                finish();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.start();
    }
}
